package com.fx.ruankao.controller;

import com.fx.ruankao.entity.QuestionBank;
import com.fx.ruankao.service.QuestionBankService;
import com.fx.ruankao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 题库控制器 - 管理端
 */
@RestController
@RequestMapping("/api/admin/questions")
public class QuestionBankController {
    
    @Autowired
    private QuestionBankService questionBankService;
    
    /**
     * 新增题目
     */
    @PostMapping
    public Result insert(@RequestBody QuestionBank questionBank) {
        try {
            int result = questionBankService.insert(questionBank);
            if (result > 0) {
                return Result.success("新增题目成功");
            } else {
                return Result.error("新增题目失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除题目
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            int result = questionBankService.deleteById(id);
            if (result > 0) {
                return Result.success("删除题目成功");
            } else {
                return Result.error("删除题目失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 更新题目
     */
    @PutMapping
    public Result update(@RequestBody QuestionBank questionBank) {
        try {
            int result = questionBankService.update(questionBank);
            if (result > 0) {
                return Result.success("更新题目成功");
            } else {
                return Result.error("更新题目失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询题目
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        try {
            QuestionBank questionBank = questionBankService.selectById(id);
            if (questionBank != null) {
                return Result.success(questionBank);
            } else {
                return Result.error("未找到题目");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有题目
     */
    @GetMapping
    public Result selectAll() {
        try {
            List<QuestionBank> questionBanks = questionBankService.selectAll();
            return Result.success(questionBanks);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除题目
     */
    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody Integer[] ids) {
        try {
            int result = questionBankService.batchDelete(ids);
            if (result > 0) {
                return Result.success("批量删除题目成功");
            } else {
                return Result.error("批量删除题目失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据题目类型查询题目
     */
    @GetMapping("/type/{questionType}")
    public Result selectByQuestionType(@PathVariable String questionType) {
        try {
            List<QuestionBank> questionBanks = questionBankService.selectByQuestionType(questionType);
            return Result.success(questionBanks);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据科目查询题目
     */
    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable String subject) {
        try {
            List<QuestionBank> questionBanks = questionBankService.selectBySubject(subject);
            return Result.success(questionBanks);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据难度查询题目
     */
    @GetMapping("/difficulty/{difficulty}")
    public Result selectByDifficulty(@PathVariable String difficulty) {
        try {
            List<QuestionBank> questionBanks = questionBankService.selectByDifficulty(difficulty);
            return Result.success(questionBanks);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 随机获取指定数量的题目
     */
    @GetMapping("/random")
    public Result getRandomQuestions(
            @RequestParam int count,
            @RequestParam(required = false) String questionType,
            @RequestParam(required = false) String subject,
            @RequestParam(required = false) String difficulty) {
        try {
            List<QuestionBank> questionBanks = questionBankService.getRandomQuestions(
                    count, questionType, subject, difficulty);
            return Result.success(questionBanks);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 导入题目（简单实现）
     */
    @PostMapping("/import")
    public Result importQuestions(@RequestParam("file") MultipartFile file) {
        try {
            // 这里简化处理，实际应该根据文件类型（Excel、CSV等）解析文件内容
            // 然后调用questionBankService.batchImport方法导入
            return Result.success("导入成功，实际应用中需要解析文件内容");
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    

}